<?php
class MyUtility
{
    public static function highlightText($string, $words)
    {
            foreach ( $words as $word )
            {
                $string = str_ireplace($word, '<span class="highlight_word">'.$word.'</span>', $string);
            }
            /*** return the highlighted string ***/
            return $string;
    }

    public static function highlightWords($text, $words)
    {
            /*** loop of the array of words ***/
            foreach ($words as $word)
            {
                /*** quote the text for regex ***/
                $word = preg_quote($word);
                /*** highlight the words ***/
                $text = preg_replace("/\b($word)\b/i", '<span class="highlight_word">\1</span>', $text);
            }
            /*** return the text ***/
            return $text;
    }

    public static function slugify($text)
    {
        // replace all non letters or digits by -
        $text = preg_replace('/\W+/', '-', $text);
        // trim and lowercase
        $text = strtolower(trim($text, '-'));

        if (empty($text))
        {
            return 'n-a';
        }
        else
        {
            return $text;
        }
    }
    
    public static function my_nofollow($content)
    {
        return str_ireplace('<a', '<a rel="nofollow" ', $content);
            //return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
    }

    public static function my_nofollow_callback($matches)
    {    
        $link = $matches[0];
        $site_link = "http://localhost:8083/";

        if (strpos($link, 'rel') === false)
        {
            $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
        }
        elseif (preg_match("%href=\S(?!$site_link)%i", $link))
        {
            $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
        }
        
        return $link;
     }

     public static function countOccurence($tag, $content)
     {
         $occurence = 0;
         preg_match('/<'.$tag.'[^>]+/', $content, $matches);
         $occurence = count($matches);
         return $occurence;
     }
}
?>